<?php

declare(strict_types=1);

namespace app\admin\controller;

use app\admin\controller\Base;
use think\Request;

/**
 * 交易币种控制器
 */
class TransactionCurrency extends Base
{

    protected $request;
    protected $service;
    protected $validate;

    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->service = new \app\admin\service\TransactionCurrency();
        $this->validate = new \app\admin\validate\TransactionCurrency();
    }

    /**
     * @describe 获取交易币种列表
     * @author: Bobo
     * @date: 2024-09-14 03:04:33 
     * @param [] type
     * @return Renderable
     */
    public function index()
    {
        $params = $this->request->params;
        if (!$this->validate->scene('search')->check($params)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }
        $pageIndex  = isset($params['pageIndex'])    ? intval($params['pageIndex']) : 1;
        $pageSize   = isset($params['pageSize'])     ? intval($params['pageSize']) : 10;
        $country    = isset($params['country'])      ? trim($params['country']) : null;
        $name       = isset($params['name'])         ? trim($params['name']) : null;
        $abbr       = isset($params['abbr'])         ? trim($params['abbr']) : null;
        $status     = isset($params['status'])       ? intval($params['status']) : null;

        $where = ['is_del' => 0];
        if ($country) {
            $where[] = ['country', 'like', '%' . $country . '%'];
        }
        if ($name) {
            $where[] = ['name', 'like', '%' . $name . '%'];
        }
        if ($abbr) {
            $where[] = ['abbr', 'like', '%' . $abbr . '%'];
        }
        if ($status > -1) {
            $where['status'] = $status;
        }

        $res = $this->service->getList($where, $pageIndex, $pageSize);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }
    /**
     * @describe 获取交易币种详情
     * @author: Bobo
     * @date: 2024-09-14 03:07:37 
     * @param [int] $id 交易币种ID @required
     * @return Renderable
     */
    public function show()
    {
        $params = $this->request->params;
        if (!$this->validate->scene('show')->check($params)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }
        $res  = $this->service->getOneById($params['id']);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * @describe 创建交易币种
     * @author: Bobo
     * @date: 2024-09-14 03:09:17 
     * @param [array] $data 创建交易币种的数据 @required
     * @return Renderable
     */
    public function create()
    {
        $data = $this->request->params;

        if (!$this->validate->scene('create')->check($data)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }

        $data['create_time'] = date('Y-m-d H:i:s');
        $data['creator'] = $this->request->operator_id;

        $result = $this->service->create($data);
        $code = $result['code'];
        $data = $result['data'] ?? [];
        $message = $result['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * 更新交易币种
     * @describe 更新交易币种
     * @author: Bobo
     * @date: 2024-09-14 03:11:17 
     * @param [int] $id 交易币种ID @required
     * @param [array] $data 更新交易币种的数据 @required
     * @return Renderable
     */
    public function update()
    {

        $params = $this->request->params;

        if (!$this->validate->scene('update')->check($params)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }
        $result = $this->service->update($params);

        $code = $result['code'];
        $data = $result['data'] ?? [];
        $message = $result['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * 删除交易币种
     * @param int $id 交易币种ID
     */
    /**
     * @describe 删除交易币种
     * @author: Bobo
     * @date: 2024-09-14 03:13:56 
     * @param [int] $id 交易币种ID @required
     * @return Renderable
     */
    public function delete()
    {
        $params = $this->request->params;
        if (!$this->validate->scene('delete')->check($params)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }
        $ids =  trim($params['ids']);
        $res = $this->service->batchLogicalDelete($ids);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * @describe 
     * @author: Bobo
     * @date: 2024-08-06 19:07:39 
     * @param [type] @required
     * @return Renderable
     */
    public function getFieldEnum()
    {
        $params = $this->request->params;
        $res = $this->service->getFieldEnum($params['field']);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return
            $this->returnJsonData($code, $data, $message);
    }


    /**
     * @describe 切换交易币种状态
     * @author: Bobo
     * @date: 2024-08-28 13:57:39 
     * @param [type] @required
     * @return Renderable
     */
    public function status()
    {
        $params = $this->request->params;
        if (!$this->validate->scene('update')->check($params)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }
        $params['updater'] = $this->request->operator_id;
        $params['update_time'] = time();
        $res = $this->service->status($params);

        $code = $res['code'];
        $data = $res['data'];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * @describe
     * @author: Bobo
     * @date: 2024-09-14 14:28:28 
     * @param [type] @required
     * @return Renderable
     */
    public function getSelectOption()
    {
        $params = $this->request->params;
        $where = ['status' => 1, 'is_del' => 0];

        $res = $this->service->getSelectOption($where);
        $code = $res['code'];
        $data = $res['data'];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }
}
